package com.base.work.adapter;

import android.os.Build;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.exception.HandlerException;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.recy.BasicRecyViewHolder;
import com.base.commonlib.recy.HFSingleTypeRecyAdapter;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.ToastUtil;
import com.base.work.R;
import com.base.work.bean.WorkEntity;

/**
 * 在此写用途
 *
 * @version V1.0 <描述当前版本功能>
 *          Created by admin on 2018/5/22.
 * @FileName: com.zhongyue.mdchengguan.ui.main.adapter.CategoryAdapter.java
 * Author weikailiang
 * @date: 2018-05-22 17:31
 */
public class CategoryAdapter extends HFSingleTypeRecyAdapter<WorkEntity,CategoryAdapter.RecyViewHolder> {


    public CategoryAdapter(int resId) {
        super(resId);
    }

    @Override
    public RecyViewHolder buildViewHolder(View itemView) {
        return new RecyViewHolder(itemView);
    }

    @RequiresApi(api = Build.VERSION_CODES.DONUT)
    @Override
    public void bindDataToHolder(RecyViewHolder holder, final WorkEntity bean, int position) {
        if (TextUtils.isEmpty(bean.getLoadPic())){
            holder.mIvImg.setImageResource(holder.mIvImg.getContext().getResources().getIdentifier(bean.getWebUrl(),"mipmap",holder.mIvImg.getContext().getApplicationInfo().packageName));
        }else {
            if (bean.getLoadPic().startsWith("http://")) {
                GlideUtil.load(holder.mIvImg.getContext(), bean.getLoadPic(), holder.mIvImg);
            }else {
                holder.mIvImg.setImageResource(holder.mIvImg.getContext().getResources().getIdentifier(bean.getLoadPic(),"mipmap",holder.mIvImg.getContext().getApplicationInfo().packageName));
            }
        }
        holder.mTvName.setText(bean.getTextInfo());
//            if (!TextUtils.isEmpty(bean.get())) {
//                if (!bean.getNum().equals("0")){
//                    holder.mJiaoBiao.setVisibility(View.VISIBLE);
//                    holder.mJiaoBiao.setText(bean.getNum());
//                }
//            }
        holder.mRlCate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean.isCustom()) {
                    ARouter.getInstance().build(bean.getmClass())
                    .withString("ID",bean.getId())
                    .withInt("num",bean.getNum())
                    .withString("type",bean.getType())
                    .withBoolean("isCase",bean.isCase())
                    .navigation();

                } else {

                    if (bean.getWebUrl().startsWith("http")) {
                        ARouter.getInstance().build("/sweb/common")
                                .withInt("key", 1)
                                .withString("url", bean.getWebUrl())
                                .withString("title", "江苏中越科技")
                                .withString("name", "测试")
                                .navigation();
                    } else {
                        try {

                            if (bean.getmClass().equals("todone")) {
                                ARouter.getInstance().build("/lpark/memory")
                                        .withString("ResultActivity", "/crpuct/vehicleup")
                                        .withString("id", bean.getId())
                                        .withString("PATH", Environment.getExternalStorageDirectory().getAbsolutePath() + "/lpark/park")
                                        .navigation();
                            } else if (bean.getmClass().equals("todotwo")) {
                                ARouter.getInstance().build("/lpark/memory")
                                        .withString("ResultActivity", "/crpuct/community")
                                        .withString("id", bean.getId())
                                        .withString("PATH", Environment.getExternalStorageDirectory().getAbsolutePath() + "/lpark/park")
                                        .navigation();
                            } else {
                                ARouter.getInstance().build(bean.getmClass())
                                        .withString("id", bean.getId())
                                        .navigation();
                            }
                        } catch (HandlerException e) {
                            ToastUtil.show("暂无权限");
                        }

                    }
                }
            }
        });
    }

    public static class RecyViewHolder extends BasicRecyViewHolder {
        ImageView mIvImg;
        TextView mTvName;
        TextView mJiaoBiao;
        RelativeLayout mRlCate;
        public RecyViewHolder(View itemView) {
            super(itemView);
            mIvImg =  itemView.findViewById(R.id.iv_img);
            mTvName = itemView.findViewById(R.id.tv_name);
            mJiaoBiao = itemView.findViewById(R.id.jiaobiao);
            mRlCate = itemView.findViewById(R.id.ll_cate);
        }
    }
}
